package com.java.basis.io.stream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;

/**
 * 文件复制
 * @author wangning
 * @create 2021-06-15 10:28
 */
public class TestFileCopy {
	public static void main(String[] args) {
		System.out.println("源文件：");
		Scanner scanner = new Scanner(System.in);
//		String s = new Scanner(System.in).nextLine();
		String s1 = scanner.nextLine();
		File from = new File(s1);
		if(!from.isFile()) {
			System.out.println("输入的不是文件！");
			return;
		}
		System.out.println(from.getAbsolutePath());
		System.out.println(from.getPath());
		System.out.println(from.getParent());
		System.out.println(from.getName());
		System.out.println("目标文件：");
		String s2 = scanner.nextLine();
		File to = new File(s2);
		scanner.close();
		try {
			copy(from, to);
			System.out.println("完成");

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("失败");
		}

	}

	private static void copy(File from, File to) throws Exception {
		FileInputStream in = new FileInputStream(from);
		FileOutputStream out = new FileOutputStream(to);
		byte[] buff = new byte[8192];
		int n;//每一批的数量
		while ((n = in.read(buff)) != -1) {
			out.write(buff, 0, n);
		}
		out.close();
		in.close();
	}

}
